<!DOCTYPE html>
<html lang="en" dir="auto">

<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script><meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="robots" content="noindex, nofollow">
<title>地址 | My New Hugo Site</title>
<meta name="keywords" content="">
<meta name="description" content="读操作
CPU发送访问地址
判断是否命中


否

判断缓存是否已满



是


写操作

写直达法
写回法

Cache块退出时进行替换，将其写回主存
对希望替换的值进行上锁



地址映射表（位置在缓存中）
全相联地址映射
概念：
字块
过程：


划分字块


将主存和Cache划分出一部分大小相同，数量不等的字块


将字块进行编号，称为主存块号和Cache块号


主存块号的编写方式通过将地址分成两部分：块编号&#43;块内地址（字块）


Cache块号编写方式通过将Cache编号&#43;块内地址（字块）




构建地址映射表



  
      
          缓存块号（共2^M^个）
          主存块号（位数为N）
          V（有效位，大小为1）
      
  
  
      
          0
          20845H
          1（已使用）
      
      
          1
          
          0
      
      
          2
          
          0
      
      
          &hellip;
          
          &hellip;
      
      
          n
          42851H
          1
      
  



映射表的大小：(N&#43;1)*2^M^




缺点：
访问速度不平均
直接地址映射
过程：


划分存储


将主存以Cache容量大小划分出大小相同的区


将字块进行编号，称为区内块号


主存块号的编写方式通过将地址分为三部分：区号&#43;区内块号&#43;块内地址（字块）


Cache块号编写方式通过将Cache编号&#43;块内地址（字块）




构建地址映射表



  
      
          （主存）区号（位数为N）
          （主存）区内块号
          缓存块号（共2^M^个）
          V（有效位，大小为1）
      
  
  
      
          20845H
          0
          0
          1
      
      
          
          1
          1
          0
      
      
          
          2
          2
          0
      
      
          
          &hellip;
          &hellip;
          &hellip;
      
      
          42851H
          n
          n
          1
      
  



缺点：
容易频繁替换">
<meta name="author" content="">
<link rel="canonical" href="http://localhost:1313/%E5%9C%B0%E5%9D%80/">
<link crossorigin="anonymous" href="/assets/css/stylesheet.6da9a63d25a9608bca2f7f907a030e887a7dd3c3f3918e4cc113129361414bda.css" integrity="sha256-bammPSWpYIvKL3&#43;QegMOiHp908PzkY5MwRMSk2FBS9o=" rel="preload stylesheet" as="style">
<link rel="icon" href="http://localhost:1313/favicon.ico">
<link rel="icon" type="image/png" sizes="16x16" href="http://localhost:1313/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="http://localhost:1313/favicon-32x32.png">
<link rel="apple-touch-icon" href="http://localhost:1313/apple-touch-icon.png">
<link rel="mask-icon" href="http://localhost:1313/safari-pinned-tab.svg">
<meta name="theme-color" content="#2e2e33">
<meta name="msapplication-TileColor" content="#2e2e33">
<link rel="alternate" hreflang="en" href="http://localhost:1313/%E5%9C%B0%E5%9D%80/">
<noscript>
    <style>
        #theme-toggle,
        .top-link {
            display: none;
        }

    </style>
    <style>
        @media (prefers-color-scheme: dark) {
            :root {
                --theme: rgb(29, 30, 32);
                --entry: rgb(46, 46, 51);
                --primary: rgb(218, 218, 219);
                --secondary: rgb(155, 156, 157);
                --tertiary: rgb(65, 66, 68);
                --content: rgb(196, 196, 197);
                --code-block-bg: rgb(46, 46, 51);
                --code-bg: rgb(55, 56, 62);
                --border: rgb(51, 51, 51);
            }

            .list {
                background: var(--theme);
            }

            .list:not(.dark)::-webkit-scrollbar-track {
                background: 0 0;
            }

            .list:not(.dark)::-webkit-scrollbar-thumb {
                border-color: var(--theme);
            }
        }

    </style>
</noscript>
</head>

<body class="" id="top">
<script>
    if (localStorage.getItem("pref-theme") === "dark") {
        document.body.classList.add('dark');
    } else if (localStorage.getItem("pref-theme") === "light") {
        document.body.classList.remove('dark')
    } else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
        document.body.classList.add('dark');
    }

</script>

<header class="header">
    <nav class="nav">
        <div class="logo">
            <a href="http://localhost:1313/" accesskey="h" title="My New Hugo Site (Alt + H)">My New Hugo Site</a>
            <div class="logo-switches">
                <button id="theme-toggle" accesskey="t" title="(Alt + T)" aria-label="Toggle theme">
                    <svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
                        fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                        stroke-linejoin="round">
                        <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
                    </svg>
                    <svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
                        fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                        stroke-linejoin="round">
                        <circle cx="12" cy="12" r="5"></circle>
                        <line x1="12" y1="1" x2="12" y2="3"></line>
                        <line x1="12" y1="21" x2="12" y2="23"></line>
                        <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
                        <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
                        <line x1="1" y1="12" x2="3" y2="12"></line>
                        <line x1="21" y1="12" x2="23" y2="12"></line>
                        <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
                        <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
                    </svg>
                </button>
            </div>
        </div>
        <ul id="menu">
            <li>
                <a href="http://localhost:1313/" title="首页">
                    <span>首页</span>
                </a>
            </li>
            <li>
                <a href="http://localhost:1313/about/" title="关于">
                    <span>关于</span>
                </a>
            </li>
        </ul>
    </nav>
</header>
<main class="main">

<article class="post-single">
  <header class="post-header">
    <div class="breadcrumbs"><a href="http://localhost:1313/">Home</a></div>
    <h1 class="post-title entry-hint-parent">
      地址
    </h1>
    <div class="post-meta"><span title='2024-05-03 12:00:00 +0800 CST'>May 3, 2024</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;83 words

</div>
  </header> <div class="toc">
    <details >
        <summary accesskey="c" title="(Alt + C)">
            <span class="details">Table of Contents</span>
        </summary>

        <div class="inner"><ul><ul>
                <li>
                    <a href="#%e8%af%bb%e6%93%8d%e4%bd%9c" aria-label="读操作">读操作</a></li>
                <li>
                    <a href="#%e5%86%99%e6%93%8d%e4%bd%9c" aria-label="写操作">写操作</a></li></ul>
                    
                <li>
                    <a href="#%e5%9c%b0%e5%9d%80%e6%98%a0%e5%b0%84%e8%a1%a8%e4%bd%8d%e7%bd%ae%e5%9c%a8%e7%bc%93%e5%ad%98%e4%b8%ad" aria-label="地址映射表（位置在缓存中）">地址映射表（位置在缓存中）</a><ul>
                        
                <li>
                    <a href="#%e5%85%a8%e7%9b%b8%e8%81%94%e5%9c%b0%e5%9d%80%e6%98%a0%e5%b0%84" aria-label="全相联地址映射">全相联地址映射</a><ul>
                        
                <li>
                    <a href="#%e6%a6%82%e5%bf%b5" aria-label="概念：">概念：</a></li>
                <li>
                    <a href="#%e8%bf%87%e7%a8%8b" aria-label="过程：">过程：</a></li>
                <li>
                    <a href="#%e7%bc%ba%e7%82%b9" aria-label="缺点：">缺点：</a></li></ul>
                </li>
                <li>
                    <a href="#%e7%9b%b4%e6%8e%a5%e5%9c%b0%e5%9d%80%e6%98%a0%e5%b0%84" aria-label="直接地址映射">直接地址映射</a><ul>
                        
                <li>
                    <a href="#%e8%bf%87%e7%a8%8b-1" aria-label="过程：">过程：</a></li>
                <li>
                    <a href="#%e7%bc%ba%e7%82%b9-1" aria-label="缺点：">缺点：</a></li></ul>
                </li>
                <li>
                    <a href="#%e7%bb%84%e7%9b%b8%e8%bf%9e%e5%9c%b0%e5%9d%80%e6%98%a0%e5%b0%84" aria-label="组相连地址映射">组相连地址映射</a><ul>
                        
                <li>
                    <a href="#%e8%bf%87%e7%a8%8b-2" aria-label="过程：">过程：</a></li>
                <li>
                    <a href="#%e4%bc%98%e7%82%b9" aria-label="优点：">优点：</a></li></ul>
                </li></ul>
                </li>
                <li>
                    <a href="#%e6%9b%bf%e6%8d%a2%e7%ae%97%e6%b3%95" aria-label="替换算法">替换算法</a><ul>
                        <ul>
                        
                <li>
                    <a href="#%e5%85%88%e8%bf%9b%e5%85%88%e5%87%ba%e9%98%9f%e5%88%97" aria-label="先进先出（队列）">先进先出（队列）</a></li>
                <li>
                    <a href="#%e8%bf%91%e6%9c%9f%e6%9c%80%e5%b0%91%e4%bd%bf%e7%94%a8%e5%9f%ba%e4%ba%8e%e8%ae%bf%e9%97%ae%e6%ac%a1%e6%95%b0%e7%bb%9f%e8%ae%a1" aria-label="近期最少使用（基于访问次数统计）">近期最少使用（基于访问次数统计）</a>
                </li>
            </ul>
            </ul>
            </li>
            </ul>
        </div>
    </details>
</div>

  <div class="post-content"><h4 id="读操作">读操作<a hidden class="anchor" aria-hidden="true" href="#读操作">#</a></h4>
<p>CPU发送访问地址</p>
<p>判断是否命中</p>
<ul>
<li>
<p>否</p>
<ul>
<li>判断缓存是否已满</li>
</ul>
</li>
<li>
<p>是</p>
</li>
</ul>
<h4 id="写操作">写操作<a hidden class="anchor" aria-hidden="true" href="#写操作">#</a></h4>
<ol>
<li>写直达法</li>
<li>写回法
<ol>
<li>Cache块退出时进行替换，将其写回主存</li>
<li>对希望替换的值进行上锁</li>
</ol>
</li>
</ol>
<h3 id="地址映射表位置在缓存中">地址映射表（位置在缓存中）<a hidden class="anchor" aria-hidden="true" href="#地址映射表位置在缓存中">#</a></h3>
<h4 id="全相联地址映射">全相联地址映射<a hidden class="anchor" aria-hidden="true" href="#全相联地址映射">#</a></h4>
<h5 id="概念">概念：<a hidden class="anchor" aria-hidden="true" href="#概念">#</a></h5>
<p>字块</p>
<h5 id="过程">过程：<a hidden class="anchor" aria-hidden="true" href="#过程">#</a></h5>
<ul>
<li>
<p>划分字块</p>
<ul>
<li>
<p>将主存和Cache划分出一部分大小相同，数量不等的字块</p>
</li>
<li>
<p>将字块进行编号，称为主存块号和Cache块号</p>
</li>
<li>
<p>主存块号的编写方式通过将地址分成两部分：块编号+块内地址（字块）</p>
</li>
<li>
<p>Cache块号编写方式通过将Cache编号+块内地址（字块）</p>
</li>
</ul>
</li>
<li>
<p>构建地址映射表</p>
<ul>
<li>
<table>
  <thead>
      <tr>
          <th>缓存块号（共2^M^个）</th>
          <th>主存块号（位数为N）</th>
          <th>V（有效位，大小为1）</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>0</td>
          <td>20845H</td>
          <td>1（已使用）</td>
      </tr>
      <tr>
          <td>1</td>
          <td></td>
          <td>0</td>
      </tr>
      <tr>
          <td>2</td>
          <td></td>
          <td>0</td>
      </tr>
      <tr>
          <td>&hellip;</td>
          <td></td>
          <td>&hellip;</td>
      </tr>
      <tr>
          <td>n</td>
          <td>42851H</td>
          <td>1</td>
      </tr>
  </tbody>
</table>
</li>
<li>
<p>映射表的大小：(N+1)*2^M^</p>
</li>
</ul>
</li>
</ul>
<h5 id="缺点">缺点：<a hidden class="anchor" aria-hidden="true" href="#缺点">#</a></h5>
<p>访问速度不平均</p>
<h4 id="直接地址映射">直接地址映射<a hidden class="anchor" aria-hidden="true" href="#直接地址映射">#</a></h4>
<h5 id="过程-1">过程：<a hidden class="anchor" aria-hidden="true" href="#过程-1">#</a></h5>
<ul>
<li>
<p>划分存储</p>
<ul>
<li>
<p>将主存以Cache容量大小划分出大小相同的区</p>
</li>
<li>
<p>将字块进行编号，称为区内块号</p>
</li>
<li>
<p>主存块号的编写方式通过将地址分为<strong>三部分</strong>：区号+区内块号+块内地址（字块）</p>
</li>
<li>
<p>Cache块号编写方式通过将Cache编号+块内地址（字块）</p>
</li>
</ul>
</li>
</ul>
<p>构建地址映射表</p>
<ul>
<li>
<table>
  <thead>
      <tr>
          <th>（主存）区号（位数为N）</th>
          <th>（主存）区内块号</th>
          <th>缓存块号（共2^M^个）</th>
          <th>V（有效位，大小为1）</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>20845H</td>
          <td>0</td>
          <td>0</td>
          <td>1</td>
      </tr>
      <tr>
          <td></td>
          <td>1</td>
          <td>1</td>
          <td>0</td>
      </tr>
      <tr>
          <td></td>
          <td>2</td>
          <td>2</td>
          <td>0</td>
      </tr>
      <tr>
          <td></td>
          <td>&hellip;</td>
          <td>&hellip;</td>
          <td>&hellip;</td>
      </tr>
      <tr>
          <td>42851H</td>
          <td>n</td>
          <td>n</td>
          <td>1</td>
      </tr>
  </tbody>
</table>
</li>
</ul>
<h5 id="缺点-1">缺点：<a hidden class="anchor" aria-hidden="true" href="#缺点-1">#</a></h5>
<p>容易频繁替换</p>
<h4 id="组相连地址映射">组相连地址映射<a hidden class="anchor" aria-hidden="true" href="#组相连地址映射">#</a></h4>
<h5 id="过程-2">过程：<a hidden class="anchor" aria-hidden="true" href="#过程-2">#</a></h5>
<ul>
<li>
<p>划分存储</p>
<ul>
<li>
<p>将Cache划分成几个相同大小的路（纵向），不同路中相同位置的存储部分叫做一组（横向）</p>
</li>
<li>
<p>将主存以<strong>路的容量大小</strong>划分出大小相同的区</p>
</li>
<li>
<p>主存块号的编写方式通过将地址分为<strong>三部分</strong>：区号+区内块号+块内地址（字块）</p>
</li>
<li>
<p>Cache块号编写方式通过将Cache编号+块内地址（字块）</p>
</li>
</ul>
</li>
</ul>
<h5 id="优点">优点：<a hidden class="anchor" aria-hidden="true" href="#优点">#</a></h5>
<p>减少替换并且访问很快</p>
<h3 id="替换算法">替换算法<a hidden class="anchor" aria-hidden="true" href="#替换算法">#</a></h3>
<h5 id="先进先出队列">先进先出（队列）<a hidden class="anchor" aria-hidden="true" href="#先进先出队列">#</a></h5>
<h5 id="近期最少使用基于访问次数统计">近期最少使用（基于访问次数统计）<a hidden class="anchor" aria-hidden="true" href="#近期最少使用基于访问次数统计">#</a></h5>


  </div>

  <footer class="post-footer">
    <ul class="post-tags">
    </ul>

  </footer>
</article>
    </main>
    
<footer class="footer">
        <span>&copy; 2025 <a href="http://localhost:1313/">My New Hugo Site</a></span> · 

    <span>
        Powered by
        <a href="https://gohugo.io/" rel="noopener noreferrer" target="_blank">Hugo</a> &
        <a href="https://github.com/adityatelange/hugo-PaperMod/" rel="noopener" target="_blank">PaperMod</a>
    </span>
</footer>
<a href="#top" aria-label="go to top" title="Go to Top (Alt + G)" class="top-link" id="top-link" accesskey="g">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentColor">
        <path d="M12 6H0l6-6z" />
    </svg>
</a>

<script>
    let menu = document.getElementById('menu')
    if (menu) {
        menu.scrollLeft = localStorage.getItem("menu-scroll-position");
        menu.onscroll = function () {
            localStorage.setItem("menu-scroll-position", menu.scrollLeft);
        }
    }

    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener("click", function (e) {
            e.preventDefault();
            var id = this.getAttribute("href").substr(1);
            if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
                document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({
                    behavior: "smooth"
                });
            } else {
                document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView();
            }
            if (id === "top") {
                history.replaceState(null, null, " ");
            } else {
                history.pushState(null, null, `#${id}`);
            }
        });
    });

</script>
<script>
    var mybutton = document.getElementById("top-link");
    window.onscroll = function () {
        if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
            mybutton.style.visibility = "visible";
            mybutton.style.opacity = "1";
        } else {
            mybutton.style.visibility = "hidden";
            mybutton.style.opacity = "0";
        }
    };

</script>
<script>
    document.getElementById("theme-toggle").addEventListener("click", () => {
        if (document.body.className.includes("dark")) {
            document.body.classList.remove('dark');
            localStorage.setItem("pref-theme", 'light');
        } else {
            document.body.classList.add('dark');
            localStorage.setItem("pref-theme", 'dark');
        }
    })

</script>
<script>
    document.querySelectorAll('pre > code').forEach((codeblock) => {
        const container = codeblock.parentNode.parentNode;

        const copybutton = document.createElement('button');
        copybutton.classList.add('copy-code');
        copybutton.innerHTML = 'copy';

        function copyingDone() {
            copybutton.innerHTML = 'copied!';
            setTimeout(() => {
                copybutton.innerHTML = 'copy';
            }, 2000);
        }

        copybutton.addEventListener('click', (cb) => {
            if ('clipboard' in navigator) {
                navigator.clipboard.writeText(codeblock.textContent);
                copyingDone();
                return;
            }

            const range = document.createRange();
            range.selectNodeContents(codeblock);
            const selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
            try {
                document.execCommand('copy');
                copyingDone();
            } catch (e) { };
            selection.removeRange(range);
        });

        if (container.classList.contains("highlight")) {
            container.appendChild(copybutton);
        } else if (container.parentNode.firstChild == container) {
            
        } else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") {
            
            codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton);
        } else {
            
            codeblock.parentNode.appendChild(copybutton);
        }
    });
</script>
</body>

</html>
